﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Milan.Reflection;

namespace Milan.Forms
{
	public partial class SplashScreen : Form
	{
		public SplashScreen()
		{
			InitializeComponent();
		}

		private void SplashScreen_Load(object sender, EventArgs e)
		{
			labelCompanyName.Text = Milan.Settings.CompanyName;
			labelProductName.Text = Milan.Settings.ProductName;
			labelVersion.Text = Milan.Settings.ProductVersion.ToString();
			labelBuildDate.Text = Milan.Diagnostics.Pe.RetrieveLinkerTimestamp(Assembly.GetEntryAssembly()).ToLocalTime().ToString("F");
			labelCopyright.Text = ((AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(Assembly.GetEntryAssembly(), typeof(AssemblyCopyrightAttribute), false)).Copyright;
			m_started.Set();
		}
		private static SplashScreen Current;
		private static Thread m_thread;
		private static ManualResetEvent m_started;
		delegate void SetStepsDelegate(int value);
		private static void SetSteps(int value)
		{
			Current.progressBarSplash.Value = value;
		}
		public static int Step
		{
			set
			{
				Current.Invoke(new SetStepsDelegate(SetSteps), value);
			}
		}
		delegate void SetNumberOfStepsDelegate(int value);
		private static void SetMaximum(int value)
		{
			Current.progressBarSplash.Maximum = value;
		}
		public static int NumberOfSteps
		{
			set
			{
				Current.Invoke(new SetStepsDelegate(SetMaximum), value);
			}
		}
		private static void MainThread()
		{
			Current = new SplashScreen();
			System.Windows.Forms.Application.Run( Current);
		}
		public static void ThrowUp()
		{
			m_started = new ManualResetEvent(false);
			m_thread = new Thread(MainThread);
			m_thread.IsBackground = true;
			m_thread.TrySetApartmentState(ApartmentState.STA);
			m_thread.Start();
			m_started.WaitOne();
		}
		delegate void CloseDelegate();
		public static void TakeDown()
		{
			Current.Invoke(new CloseDelegate(Current.Close));
		}
	}
}
